﻿using System;
using System.Collections.Generic;
using Fee.FileAttachs;
using Fee.Orders.DesignWorkflow;

namespace Fee.Orders
{
    /// <summary>
    ///     委托任务，由委托工单对任务进行 增加/修改/取消等炒作。
    /// </summary>
    /// <remarks>
    ///     如果状态已为“已验收状态”，单位不可变更，但单位对应的框架合同可变更，框架合同对应的投资项目编号可变更。
    /// </remarks>
    public class ConstructionTask
    {
        private IList<ConstructionDetail> _details;
        private IList<ConstructionTaskCostDetail> _costDetails;
        private IList<ConstructionTaskPlanCost> _planCosts;
        private IList<ConstructionTaskFileAttach> _attaches;
        public int Id { get; set; }
        public Station Station { get; set; }

        /// <summary>
        /// </summary>
        public IList<ConstructionDetail> Details
        {
            get => _details ?? (_details = new List<ConstructionDetail>());
            set => _details = value;
        }

        public IList<ConstructionTaskFileAttach> Attaches
        {
            get => _attaches ?? (_attaches = new List<ConstructionTaskFileAttach>());
            set => _attaches = value;
        }

        public IList<ConstructionTaskPlanCost> PlanCosts
        {
            get => _planCosts ?? (_planCosts = new List<ConstructionTaskPlanCost>());
            set => _planCosts = value;
        }

        public IList<ConstructionTaskCostDetail> CostDetails
        {
            get => _costDetails ?? (_costDetails = new List<ConstructionTaskCostDetail>());
            set => _costDetails = value;
        }

        /// <summary>
        /// </summary>
        public ConstructionTaskStatus Status { get; set; }


        internal ConstructionDetail GetByInfo(ConstructionType type,
            FrameworkContract contract,
            ContractType contractType)
        {
            foreach (var t in Details)
            {
                var detail = t;
                if (detail.Contract.Id == contract.Id &&
                    detail.ConstructionType == type &&
                    detail.ContractType == contractType
                )
                    return t;
            }

            return null;
        }

        /// <summary>
        ///     移除匹配的建设任务
        /// </summary>
        internal ConstructionDetail RemoveById(int detialId)
        {
            for (var i = 0; i < Details.Count; i++)
                if (Details[i].Id == detialId)
                {
                    var result = Details[i];
                    Details.RemoveAt(i);
                    return result;
                }

            return null;
        }

        public void Add(ConstructionDetail fillTo)
        {
            if (fillTo == null) throw new ArgumentNullException(nameof(fillTo));
            var details = this.Details as IList<ConstructionDetail>;
            details.Add(fillTo);
        }
    }
}